/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @headerfile debug.hpp
* 
* Header for easy log and debug.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _MOKARDU_DEBUG_HPP_
#define _MOKARDU_DEBUG_HPP_

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/**
* @class CMDebug
* @brief Debugging and logging.
*/

class CMDebug
{

	private:
		/**
		* The file for logging.
		*/
		char* filename;
		
		/**
		* Debugging or not.
		*/
		bool bdebug;
		
		/**
		* Format a variable with a correct time format for logging.
		*
		* @param[out] thetime The pointer to fill with the correct time format.
		*/
		void getTime(char *thetime);

		/**
		* Write an error to the log file.
		*
		* @param[in] error The error explained.
		* @param[in] errorfile The file who throws the error.
		* @param[in] errorfunction The function who throws the error.
		* @param[in] errorline The line who throws the error.
		*/
		void errorLog(const char* error,const char* errorfile,const char* errorfunction,int errorline);

		/**
		* Shows an error information.
		*
		* @param[in] error The error explained.
		* @param[in] errorfile The file who throws the error.
		* @param[in] errorfunction The function who throws the error.
		* @param[in] errorline The line who throws the error.
		*/
		void errorShow(const char* error,const char* errorfile,const char* errorfunction,int errorline);
		
		/**
		* Write a debug information to the log file.
		*
		* @param[in] debug The debug information.
		*/
		void debugLog(const char* debug);
		
		/**
		* Shows a debug information.
		*
		* @param[in] debug The debug information.
		*/
		void debugShow(const char* debug);	

	
	public:
		
		/**
		* Constructor
		*
		* @param[in] debug The boolean indicates if debug is activated.
		* @param[in] filename The file for logging.
		*/
		CMDebug(bool debug,const char* filename);
		
		/**
		* Destructor
		*/
		~CMDebug();

		/**
		* Makes an error log if filename != "" and show the error through stderr.
		*
		* @param[in] error The error explained.
		* @param[in] errorfile The file who throws the error.
		* @param[in] errorfunction The function who throws the error.
		* @param[in] errorline The line who throws the error.
		*/
		void error(const char* error,const char* errorfile,const char* errorfunction,int errorline);
		
		/**
		* Makes a debug log if filename != "" and debug == true\n
		* shows a debuf info through stdout if debug == true.
		*
		* @param[in] debug The debug information.
		*/
		void debug(const char* debug);

};

#endif // _MOKARDU_DEBUG_HPP_
